/**
 * 
 */
package com.antilia.infomaresme.provider;

import java.util.Iterator;

import org.apache.wicket.extensions.markup.html.repeater.data.table.ISortableDataProvider;
import org.apache.wicket.extensions.markup.html.repeater.util.SortableDataProvider;
import org.apache.wicket.model.IModel;

import com.antilia.infomaresme.InfomaresmeApplication;
import com.antilia.infomaresme.data.dao.interfaces.IImageInfosDao;
import com.antilia.infomaresme.data.dataobjects.ImageInfo;
import com.antilia.infomaresme.data.dataobjects.Item;

/**
 * @author Ernesto
 *
 */
public abstract class ImageInfosDataProvider extends SortableDataProvider<ImageInfo>  implements ISortableDataProvider<ImageInfo> {

	private static final long serialVersionUID = 1L;
		
	/**
	 * 
	 */
	public ImageInfosDataProvider() {
		
	}

	/* (non-Javadoc)
	 * @see org.apache.wicket.model.IDetachable#detach()
	 */
	public void detach() {

	}

	/* (non-Javadoc)
	 * @see org.apache.wicket.markup.repeater.data.IDataProvider#iterator(int, int)
	 */
	public Iterator<? extends ImageInfo> iterator(int first, int count) {
		//boolean isAscending = (getSort() != null && getSort().getProperty()==null) || (getSort() != null && getSort().isAscending());
		return getIItemsDao().getImageInfos(getItem(), first, count).iterator();
	}

	/* (non-Javadoc)
	 * @see org.apache.wicket.markup.repeater.data.IDataProvider#size()
	 */
	public int size() {
		return getIItemsDao().countImageInfos(getItem()).intValue();
	}

	/* (non-Javadoc)
	 * @see org.apache.wicket.markup.repeater.data.IDataProvider#model(java.lang.Object)
	 */
	public IModel<ImageInfo> model(ImageInfo object) {
		return new ImageInfosLdm(object) {
			
			private static final long serialVersionUID = 1L;

			@Override
			protected IImageInfosDao getImageInfosDao() {
				return ImageInfosDataProvider.this.getIItemsDao();
			}
		};
	}

	protected final IImageInfosDao getIItemsDao() {
		return InfomaresmeApplication.getQAApplication().getImageInfosDao();
	}
	
	public abstract Item getItem();

}
